package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.*;
import com.example.dto.DishDto;
import com.example.dto.SetmealDto;
import com.example.service.CategoryService;
import com.example.service.SetmealDishService;
import com.example.service.SetmealService;
import com.example.mapper.SetmealMapper;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2022-04-30 10:09:04
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public Page<Setmeal> getSetMealListForPage(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        this.page(setmealPage,new LambdaQueryWrapper<Setmeal>().like(!StringUtil.isNullOrEmpty(name),Setmeal::getName,name));
        return setmealPage;
    }

    @Override
    public Page<SetmealDto> setMealToSetMealDto(Page<Setmeal> pageInfo) {
        Page<SetmealDto> setMealDtoPage = new Page<>();
        //映射除records属性中的数据
        BeanUtils.copyProperties(pageInfo,setMealDtoPage,"records");

        List<SetmealDto> collect = pageInfo.getRecords().stream().map((data) -> {
            SetmealDto setMealDto = new SetmealDto();
            BeanUtils.copyProperties(data, setMealDto);

            Long categoryId = data.getCategoryId();
            String name = categoryService.getById(categoryId).getName();
            setMealDto.setCategoryName(name);
            return setMealDto;
        }).collect(Collectors.toList());

        setMealDtoPage.setRecords(collect);
        return setMealDtoPage;
    }

    @Override
    public void setMealStatusByStatus(Integer status, Long[] ids, HttpSession session) {
        Long id1 = (Long) session.getAttribute("employee");
        for (Long id : ids) {
            boolean update = this.update(new LambdaUpdateWrapper<Setmeal>()
                    .set(Setmeal::getStatus, status)
                    .set(Setmeal::getUpdateUser,id1)
                    .set(Setmeal::getUpdateTime, LocalDateTime.now())
                    .eq(Setmeal::getId, id));
        }
    }

    @Override
    public void saveWithFlavor(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        try {
            this.save(setmeal);
            setmealDishes= setmealDishes.stream().map((item) -> {
                item.setSetmealId(setmeal.getId());
                return item;
            }).collect(Collectors.toList());
            setmealDishService.saveBatch(setmealDishes);
        } catch (Exception exception) {
            exception.printStackTrace();
        }


    }

    @Override
    public SetmealDto getByIdWithFlavor(Long id) {
        Setmeal setmeal = this.getById(id);
        List<SetmealDish> setmealDishList = setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    @Override
    public void updateWithFlavor(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        List<SetmealDish> flavors = setmealDto.getSetmealDishes();

        try {
            this.updateById(setmeal);
            setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,setmeal.getId()));
            flavors = flavors.stream().map((item) -> {
                item.setSetmealId(setmeal.getId());
                return item;
            }).collect(Collectors.toList());
            setmealDishService.saveOrUpdateBatch(flavors);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public boolean deleteByIds(Long[] ids) {
        List<Long> longs = Arrays.asList(ids);
        if (this.count(new LambdaQueryWrapper<Setmeal>()
                .in(Setmeal::getId,longs)
                .eq(Setmeal::getStatus,1))>0){
            return false;
        }
        boolean b = this.removeByIds(longs);
        for (Long setmealId : longs) {
            setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,setmealId));
        }
        return b;
    }


}




